package com.ja.openchat.viewmodels;

import android.app.Application;
import android.os.Bundle;

import com.ja.openchat.databinding.ActivityGroupChatSettingsNewBinding;
import com.ja.openchat.databinding.ActivityGroupChatSettingsSingleBinding;
import com.ja.openchat.ui.chat.group.GroupChatSettingsNewActivity;
import com.ja.openchat.ui.chat.group.GroupChatSettingsSingleActivity;
import com.ja.openchat.ui.me.QrCodeFragment;
import com.zy.openchat.core.manager.TgEvent;
import com.zy.openchat.core.model.group.GroupUserInfo;
import com.zy.openchat.core.model.group.SuperGroupFullInfo;
import com.zy.openchat.core.model.group.SuperGroupInfo;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.UserInfo;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.annotation.NonNull;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;


public class GroupChatSettingSingleViewModel extends BaseViewModel {


    private GroupChatSettingsSingleActivity groupChatSettingsNewActivity;
    private ActivityGroupChatSettingsSingleBinding binding;

    public GroupChatSettingSingleViewModel(@NonNull Application application, BaseModel mRepository) {
        super(application);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTgEvent(final TgEvent tg_event) {
        switch (tg_event.event) {
            case EUser_Td_Chat_Title_Changed: {
                //会话标题修改通知
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (groupChatSettingsNewActivity.chatInfo == chat) {
                    groupChatSettingsNewActivity.chatInfo.title = chat.title;
                    groupChatSettingsNewActivity.chatInfo.title_full_py = chat.title_full_py;
                    groupChatSettingsNewActivity.resetUI();
                }
            }
            break;
            case EUser_Td_Chat_Permissions_Changed: {
                //会话权限变更通知
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (groupChatSettingsNewActivity.chatInfo == chat) {
                    groupChatSettingsNewActivity.chatInfo.permissions = chat.permissions;
                    groupChatSettingsNewActivity.resetMembersListUI();
                    groupChatSettingsNewActivity.resetUI();
                }
            }
            break;
            case EUser_Td_Chat_Photo_Changed:
            case EUser_Td_Group_Photo_Ok: {
                //会话头像修改通知
                //群组头像下载完成
                ChatInfo chat = (ChatInfo) tg_event.data;
                if (groupChatSettingsNewActivity.chatInfo == chat) {
                    groupChatSettingsNewActivity.chatInfo.photo = chat.photo;
                    groupChatSettingsNewActivity.resetUI();
                }
            }
            break;
            case EUser_Td_Contact_Photo_Ok: {
                //联系人头像更新
//                memberIsManagerGridAdapter.notifyDataSetChanged();
//                memberGridAdapter.notifyDataSetChanged();
            }
            break;
            case EUser_Td_Group_Super_Info_Changed: {
                SuperGroupInfo info = (SuperGroupInfo) tg_event.data;
                if (info.id == groupChatSettingsNewActivity.chatInfo.superGroupId()) {
                    groupChatSettingsNewActivity.super_groupInfo = info;
                    groupChatSettingsNewActivity.resetMembersListUI();
                    groupChatSettingsNewActivity.resetMamagerMembersListUI();
                    groupChatSettingsNewActivity.resetUI();
                }
            }
            break;
            case EUser_Td_Group_Super_Full_Info_Changed: {
                long id = (long) tg_event.dataEx;
                SuperGroupFullInfo info = (SuperGroupFullInfo) tg_event.data;
                if (id == groupChatSettingsNewActivity.chatInfo.superGroupId()) {
                    //延时请求群组成员
                    binding.groupName.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            groupChatSettingsNewActivity.super_groupFullInfo = info;
                            groupChatSettingsNewActivity.resetMembersListUI();
                            groupChatSettingsNewActivity.resetMamagerMembersListUI();
                            groupChatSettingsNewActivity.resetUI();

                            //刷新列表
                            groupChatSettingsNewActivity.getSuperMembers();
                            groupChatSettingsNewActivity.getSuperAdminMembers();
                            groupChatSettingsNewActivity.getTotalSuperMembers();
                        }
                    }, 100);
                }
            }
            break;
            case EUser_Td_Group_User_Nick_Name:
                if(tg_event.data!=null&&tg_event.data instanceof GroupUserInfo) {
                    GroupUserInfo groupUserInfo = (GroupUserInfo) tg_event.data;
                    if(groupUserInfo.userId == UserInfo.getUserInfo().id){
                        groupChatSettingsNewActivity.setGroupUserNickName(groupUserInfo.nickname);
                    }
                }
                break;
        }
    }

    public void setContext(GroupChatSettingsSingleActivity groupChatSettingsNewActivity, ActivityGroupChatSettingsSingleBinding binding) {
        this.groupChatSettingsNewActivity = groupChatSettingsNewActivity;
        this.binding = binding;
    }

    public void goQrcodeActivity(ChatInfo chatInfo, SuperGroupFullInfo super_groupFullInfo) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("chatInfo",chatInfo);
        bundle.putParcelable("super_groupFullInfo",super_groupFullInfo);
        startContainerActivity(QrCodeFragment.class.getCanonicalName(),bundle);
    }
}
